package api

import (
	"gitee.com/gcom/stockdock/core/job/ths"
	"gitee.com/gcom/stockdock/core/router"
	s "gitee.com/gcom/stockdock/core/support"
	"gitee.com/gcom/stockdock/spider/csrc"
	"net/http"
)

func init() {
	router.AdminRouter.HandleFunc("/stock/updateStockListThs", updateStockListThs).Methods("GET")
	router.ApiRouter.HandleFunc("/stock/query", listStock).Methods("GET")
}

func updateStockListThs(w http.ResponseWriter, r *http.Request) {
	ctx := s.GetTracerCtx(w, r)
	ths.UpdateStockListThs(ctx)
}

// 查询股票列表
func listStock(w http.ResponseWriter, r *http.Request) {
	ctx := s.GetTracerCtx(w, r)
	q := &csrc.StockListQuery{}
	if q.Ps == 0 {
		q.Ps = 100
	}
	if !s.ParseAndValidQuery(ctx, q) {
		return
	}
	list, err := csrc.GetStockList(ctx.DB, q)
	if err != nil {
		s.ResError(ctx, 400, err)
		return
	}
	s.ResOk(ctx, list)
}
